﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using Microsoft.Practices.Prism.ViewModel;
using Sautom.ClientComunication.Commands;
using Sautom.ClientComunication.QueriesService;
using Sautom.Modules.Client.Models;

namespace Sautom.Modules.Client.Controls.ViewModels
{
    public class OrderViewerViewModel : NotificationObject
    {
        public OrderViewerViewModel(CommandBase<Guid> printOrderDetails)
        {
            PrintOrderDetails = printOrderDetails;
        }

        #region Commands
        public CommandBase<Guid> PrintOrderDetails { get; private set; }
        #endregion

        #region View properties

        public bool IsShowAirlineTicket
        {
            get { return Order != null && Order.AirlineTicket != null && Order.AirlineTicket.Id != Guid.Empty; }
        }

        private ObservableCollection<GuidStringSelected> _embasyDocs;
        public ObservableCollection<GuidStringSelected> EmbasyDocs
        {
            get { return _embasyDocs; }
            set
            {
                _embasyDocs = value;
                RaisePropertyChanged(() => EmbasyDocs);
            }
        }
        
        private OrderItemDtoOutput _order;
        public OrderItemDtoOutput Order
        {
            get { return _order; }
            set
            {
                PreSelectionAction(Order);
                _order = value;
                RaisePropertyChanged(() => Order);
                InitializeDependencies(value);
            }
        }

        private void InitializeDependencies(OrderItemDtoOutput value)
        {
            if (value != null)
            {
                EmbasyDocs = new ObservableCollection<GuidStringSelected>(value.FullDocsList.Select(
                    rec =>
                    new GuidStringSelected() { Id = rec.Id, Value = rec.Element, IsSelected = value.Docs.Contains(rec.Id) }));

                RaisePropertyChanged(() => IsShowAirlineTicket); 
                PrintOrderDetails.RaiseCanExecuteChanged();
            }
        }

        private void PreSelectionAction(OrderItemDtoOutput value)
        {
            if (value != null)
            {
                value.Docs = EmbasyDocs.Where(rec => rec.IsSelected).Select(rec => rec.Id).ToArray();
            }
        }

        #endregion
    }
}
